package com.blockchain.console.servlets.financial.zs;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.console.service.system.ConstantManage;
import com.blockchain.console.service.trading.TradingManage;
import com.blockchain.console.service.trading.entity.XlbEntity;
import com.blockchain.console.servlets.content.banner.AbstractAdvertiseServlet;
import com.blockchain.framework.config.entity.VariableBean;
import com.blockchain.framework.config.service.VariableManage;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.IntegerParser;

public class Zsgl  extends AbstractAdvertiseServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		TradingManage tsradingManage = serviceSession.getService(TradingManage.class);
		PagingResult<XlbEntity> xlbs = tsradingManage.xlbSearch();
		request.setAttribute("coinList", xlbs);
		forwardView(request, response, getClass());
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		try {
			int zsbid = IntegerParser.parse(request
					.getParameter("zsbid"));
			String zssl = request.getParameter("zssl");
			int yqrzsbid = IntegerParser.parse(request
					.getParameter("yqrzsbid"));
			String yqrzssl = request.getParameter("yqrzssl");
			
			int txyqrzs = IntegerParser.parse(request
					.getParameter("txyqrzs"));

			int czyqrzs = IntegerParser.parse(request
					.getParameter("czyqrzs"));
			
			if (zsbid > 0) {
				VariableManage variableManage = serviceSession
						.getService(VariableManage.class);
				ConstantManage constantManage = serviceSession
						.getService(ConstantManage.class);
				String key = "SYSTEM.ZSBID";
				VariableBean variableBean = variableManage.get(key);
				String value = zsbid + "";
				try {
					variableManage.setProperty(key, value);
					constantManage.addConstantLog(key, variableBean.getDescription(),
							variableBean.getValue(), value);
				} catch (Throwable t) {
					getController().prompt(request, response, PromptLevel.WARRING,
							t.getMessage());
					processGet(request, response, serviceSession);
				}
			}

			if (!StringHelper.isEmpty(zssl)) {
				VariableManage variableManage = serviceSession
						.getService(VariableManage.class);
				ConstantManage constantManage = serviceSession
						.getService(ConstantManage.class);
				String key = "SYSTEM.ZSSL";
				VariableBean variableBean = variableManage.get(key);
				String value = zssl;
				try {
					variableManage.setProperty(key, value);
					constantManage.addConstantLog(key, variableBean.getDescription(),
							variableBean.getValue(), value);
				} catch (Throwable t) {
					getController().prompt(request, response, PromptLevel.WARRING,
							t.getMessage());
					processGet(request, response, serviceSession);
				}
			}
			
			if (yqrzsbid > 0) {
				VariableManage variableManage = serviceSession
						.getService(VariableManage.class);
				ConstantManage constantManage = serviceSession
						.getService(ConstantManage.class);
				String key = "SYSTEM.YQRZSBID";
				VariableBean variableBean = variableManage.get(key);
				String value = yqrzsbid + "";
				try {
					variableManage.setProperty(key, value);
					constantManage.addConstantLog(key, variableBean.getDescription(),
							variableBean.getValue(), value);
				} catch (Throwable t) {
					getController().prompt(request, response, PromptLevel.WARRING,
							t.getMessage());
					processGet(request, response, serviceSession);
				}
			}

			if (!StringHelper.isEmpty(yqrzssl)) {
				VariableManage variableManage = serviceSession
						.getService(VariableManage.class);
				ConstantManage constantManage = serviceSession
						.getService(ConstantManage.class);
				String key = "SYSTEM.YQRZSSL";
				VariableBean variableBean = variableManage.get(key);
				String value = yqrzssl;
				try {
					variableManage.setProperty(key, value);
					constantManage.addConstantLog(key, variableBean.getDescription(),
							variableBean.getValue(), value);
				} catch (Throwable t) {
					getController().prompt(request, response, PromptLevel.WARRING,
							t.getMessage());
					processGet(request, response, serviceSession);
				}
			}
			
			if (txyqrzs > 0) {
				VariableManage variableManage = serviceSession
						.getService(VariableManage.class);
				ConstantManage constantManage = serviceSession
						.getService(ConstantManage.class);
				String key = "SYSTEM.TXYQRZS";
				VariableBean variableBean = variableManage.get(key);
				String value = txyqrzs + "";
				try {
					variableManage.setProperty(key, value);
					constantManage.addConstantLog(key, variableBean.getDescription(),
							variableBean.getValue(), value);
				} catch (Throwable t) {
					getController().prompt(request, response, PromptLevel.WARRING,
							t.getMessage());
					processGet(request, response, serviceSession);
				}
			}
			if (czyqrzs > 0) {
				VariableManage variableManage = serviceSession
						.getService(VariableManage.class);
				ConstantManage constantManage = serviceSession
						.getService(ConstantManage.class);
				String key = "SYSTEM.CZYQRZS";
				VariableBean variableBean = variableManage.get(key);
				String value = txyqrzs + "";
				try {
					variableManage.setProperty(key, value);
					constantManage.addConstantLog(key, variableBean.getDescription(),
							variableBean.getValue(), value);
				} catch (Throwable t) {
					getController().prompt(request, response, PromptLevel.WARRING,
							t.getMessage());
					processGet(request, response, serviceSession);
				}
			}
			
			sendRedirect(request, response,
					getController().getURI(request, Zsgl.class));
		} catch (ParameterException | LogicalException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			processGet(request, response, serviceSession);
		}
	}

}